/*
 * JOGRE (Java Online Gaming Real-time Engine) - Spades
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://jogre.sourceforge.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.jogre.mayi.client;

import java.util.HashMap;
import java.util.Map;

import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;

import org.jogre.common.PlayerList;
import org.jogre.mayi.common.CommMayIHandScore;
import org.jogre.mayi.common.CommMayIScorecard;

/**
 * Spades score and bags table model that extends the default table model. This
 * class was mainly created to avoid cells of the table to be editable.
 *
 * @author Garrett Lehman (Gman)
 * @version Alpha 0.2.3
 */
public class MayIScoreboard extends DefaultTableModel {

	private static final String[] columnNames = {"Name", "Score"};
	
	private Map<String, Integer> scores = new HashMap<String, Integer>();
	
	private PlayerList playerList = null;
	
	
	/**
	 * Default contructor
	 *
	 * @param data Initial data to be placed into table
	 * @param columnNames Initial columns to be placed into table
	 */
	public MayIScoreboard() {
		super();
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#isCellEditable(int, int)
	 */
	public boolean isCellEditable(int row, int column) {
		return false;
	}
	
	public void addScores(CommMayIScorecard scorecard) {
		
		for (CommMayIHandScore score : scorecard.getScores()) {
			scores.put(score.getUsername(), (scores.get(score.getUsername())+score.getScore()));
			System.out.println(score.getUsername()+": "+score.getScore());
		}
		
		this.setRowCount(0);
		
		
		for (int i = 0; i < playerList.size(); i++) {
			Object[] data = new Object[2];
			data[0] = playerList.getPlayer(i);
			data[1] = scores.get(playerList.getPlayer(i).getUser().getUsername());
			System.out.println(playerList.getPlayer(i).getUser().getUsername()+": "+scores.get(playerList.getPlayer(i).getUser().getUsername()));
			this.addRow(data);
		}			
	}
	
	public void initTable(PlayerList playerList) {
		this.playerList = playerList;
		Object[][] data = new Object[playerList.size()][2];
		
		for (int i = 0; i < playerList.size(); i++) {
			scores.put(playerList.getPlayer(i).getUser().getUsername(), 0);
			data[i][0] = playerList.getPlayer(i);
			data[i][1] = "0";
		}	
		
		this.setDataVector(data, columnNames);
	}
}
